package com.lm.security.core.validate.code;

import com.lm.security.core.properties.SecurityProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.context.request.ServletWebRequest;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * @author lm_jcf
 * @date 2018/5/2 18:18
 */
//@Component("imageCodeGenerator")
public class ImageCodeGenerator implements ValidateCodeGenerator {

    // 验证码字符集
    private static final char[] chars = {
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
            'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
    // 字符数量
    private int SIZE = 4;
    // 干扰线数量
    private int LINES = 5;
    // 宽度
    private int WIDTH = 80;
    // 高度
    private int HEIGHT = 30;
    // 字体大小
    private int FONT_SIZE = 30;
    //存活时间
    private int SURVIVAL_TIME = 60;

    @Autowired
    private SecurityProperties securityProperties;


    /** 生成随机验证码及图片
     * Object[0]验证码字符串String
     * Object[1]验证码图片  BufferedImage
     */
    @Override
    public ImageCode generate(ServletWebRequest request) {

        SIZE = ServletRequestUtils.getIntParameter(request.getRequest(),"size",securityProperties.getCode().getImage().getSize());
        LINES = ServletRequestUtils.getIntParameter(request.getRequest(),"lines",securityProperties.getCode().getImage().getLines());
        WIDTH = ServletRequestUtils.getIntParameter(request.getRequest(),"width",securityProperties.getCode().getImage().getWidth());
        HEIGHT = ServletRequestUtils.getIntParameter(request.getRequest(),"height",securityProperties.getCode().getImage().getHeight());
        FONT_SIZE = ServletRequestUtils.getIntParameter(request.getRequest(),"font_size",securityProperties.getCode().getImage().getFont_size());
        SURVIVAL_TIME = ServletRequestUtils.getIntParameter(request.getRequest(),"survival_time",securityProperties.getCode().getImage().getSurvival_time());


        StringBuffer sb = new StringBuffer();
        // 1.创建空白图片
        BufferedImage image = new BufferedImage(
                WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
        // 2.获取图片画笔
        Graphics graphic = image.getGraphics();
        // 3.设置画笔颜色
        graphic.setColor(Color.LIGHT_GRAY);
        // 4.绘制矩形背景
        graphic.fillRect(0, 0, WIDTH, HEIGHT);
        // 5.画随机字符
        Random ran = new Random();
        for (int i = 0; i <SIZE; i++) {
            // 取随机字符索引
            int n = ran.nextInt(chars.length);
            // 设置随机颜色
            graphic.setColor(getRandomColor());
            // 设置字体大小
            graphic.setFont(new Font(
                    null, Font.BOLD + Font.ITALIC, FONT_SIZE));
            Font font = graphic.getFont();
            // 画字符
            graphic.drawString(
                    chars[n] + "", i * WIDTH / SIZE, (HEIGHT-font.getSize())/2 + font.getSize());
            // 记录字符
            sb.append(chars[n]);
        }
        // 6.画干扰线
        for (int i = 0; i < LINES; i++) {
            // 设置随机颜色
            graphic.setColor(getRandomColor());
            // 随机画线
            graphic.drawLine(ran.nextInt(WIDTH), ran.nextInt(HEIGHT),
                    ran.nextInt(WIDTH), ran.nextInt(HEIGHT));
        }
        // 7.返回验证码和图片
        return new ImageCode(image, sb.toString(),SURVIVAL_TIME);
    }

    /**
     * 随机取色
     */
    public static Color getRandomColor() {
        Random ran = new Random();
        Color color = new Color(ran.nextInt(256),
                ran.nextInt(256), ran.nextInt(256));
        return color;
    }

    public SecurityProperties getSecurityProperties() {
        return securityProperties;
    }

    public void setSecurityProperties(SecurityProperties securityProperties) {
        this.securityProperties = securityProperties;
    }
}
